package com.ddxz.demo.shiro.jwt.configuration;

import org.apache.shiro.authz.Permission;
import org.apache.shiro.util.AntPathMatcher;

/**
 * class description
 *
 * @author 朱宗刚
 * @version 1.0.0
 * @date 2020/11/25
 */

public class DdxzPermission implements Permission {
    /**
     * Returns {@code true} if this current instance <em>implies</em> all the functionality and/or resource access
     * described by the specified {@code Permission} argument, {@code false} otherwise.
     * <p/>
     * <p>That is, this current instance must be exactly equal to or a <em>superset</em> of the functionality
     * and/or resource access described by the given {@code Permission} argument.  Yet another way of saying this
     * would be:
     * <p/>
     * <p>If &quot;permission1 implies permission2&quot;, i.e. <code>permission1.implies(permission2)</code> ,
     * then any Subject granted {@code permission1} would have ability greater than or equal to that defined by
     * {@code permission2}.
     *
     * @param p the permission to check for behavior/functionality comparison.
     * @return {@code true} if this current instance <em>implies</em> all the functionality and/or resource access
     * described by the specified {@code Permission} argument, {@code false} otherwise.
     */
    @Override
    public boolean implies(Permission p) {
        if(!(p instanceof DdxzPermission)) {
            return false;
        }

        if(caseSensitive) {
            return pathMatcher.match(this.path, ((DdxzPermission) p).path);
        } else {
            return pathMatcher.match(this.path.toLowerCase(), ((DdxzPermission) p).path.toLowerCase());
        }
    }

    @Override
    public String toString() {
        return path;
    }

    private String path;

    private boolean caseSensitive = false;

    public DdxzPermission(String path) {
        this.path = path;
    }

    public DdxzPermission(String path, boolean caseSensitive) {
        this.path = path;
        this.caseSensitive = caseSensitive;
    }

    private static final AntPathMatcher pathMatcher = new AntPathMatcher();
}
